package GDoc::Job::Fetch::US::Exec::En::HSS::TechStandards;

use Moose;
use WWW::Mechanize;
use HTML::TableContentParser;
use GDoc::Job;

extends 'GDoc::Job::Fetch';

sub setup {
    my $self = shift;
    $self->_baseurl( 'http://www.hss.doe.gov/nuclearsafety/ns/techstds/standard/standard.html' );
    $self->_basedir( 'http://www.hss.doe.gov/nuclearsafety/ns/techstds/standard/' );
}

sub run { 
    my $self = shift;
   
    if ( $self->data->{link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->logger->info( "fetching base " . $self->_baseurl );
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );

        my $html = $mech->content;

        # UGLY: filter out the surrounding layout tables
        $html =~ s{^.*?<!--- CONTENT --->}{}s;
        $html =~ s{<!--- /CONTENT --->.*?}{}s;

        my $tables = HTML::TableContentParser->new->parse( $html );
        foreach my $tbl( @$tables ) { 
            shift @{ $tbl->{rows} };        # throw away headers

            foreach my $row( @{ $tbl->{rows} } ) { 
                my $num = $row->{cells}[0]{data} || '';
                if ( $num =~ /DOE-(HDBK|STD|SPEC)-(\d{1,4})/ ) { 
                    my ( $prefix, $num ) = ( $1, $2 );

                    my $desc = $row->{cells}[1]{data} || '';
                    my ( $link ) = ( $desc =~ m{a href="(.*?)" .*?>PDF</a>}s );
                    next unless $link;
                    $link =~ s/\n|\r//g;
                    $desc =~ s/\n|\r|<br>//g;
                    $desc =~ s/^\s+(.*)/$1/;
                    $desc =~ s/\(\d{1,4}\s+pages\).*$//s;

                    my $job = GDoc::Job->new( class => 'Fetch::US::Exec::En::HSS::TechStandards',
                                              queue => $self->fetch_queue,
                                              data  => { prefix => $prefix,
                                                         num    => $num,
                                                         desc   => $desc,
                                                         link   => $link
                                                       },
                                            );

                    $job->submit;
                }
            }
        }
    }
}


sub _fetch_pdf { 
    my $self = shift;
    my %data = %{ $self->{data} };

    my $url = $self->_basedir . $data{link};
    $self->_mech->get( $url );

    my $ident = sprintf( 'DOE-%s-%04d', $data{prefix}, $data{num} );
    my $filename = $ident . '.pdf';
    my $key = 'us/exec/en/hss/' . $filename;

    $self->logger->info( "storing $url  --> $key" );

    $self->store_pdf( key      => $key, 
                      data     => $self->_mech->response->content,
                      filename => $filename,
                      meta     => { description => $data{desc},
                                    identifier  => $ident 
                                  }
                     );
    
}

1;

